--[[
	SORAKA SLACKER v3
	By Ivan[russia]
	Credits To Zynox/grey autolvl spell/ikita autosilence/nanja taric/wee Ward Scanner
	I Learned From Their Previous Scripts
]]

-- SETTINGS --
SetupInterval = 100 -- Interval of checking, if you are laging with this script increase SetupInterval. If you need more effectiveness decrease SetupInterval
SetupAutoUlt = true -- Auto Ult On
ultEffect = 3 -- # men to minimum ult(between 1-5 best)
SetupAutoHeal = true -- Auto Heal
SetupDontHealIgnited = true -- should you heal and ult ignited targets?
SetupAutoSilence = true -- Auto Silence
SetupAutoMana = true -- Auto Mana
SetupAutoLvLSpell = true -- AutoLvL Spells of soraka
SetupManaGiveRate = 2 -- At which rate you should give mana. Example: you give 50 mana, rate 2 means 50x2, so you give mana when target miss 100 mana. Check 1 rate if you want give mana often.
distance = 850 -- How much distance to use spells
SetupTogleKey = 32 --Key to Togle script. [Space - 32] default

SORAKA_RUN = 0
SORAKA_HEAL = 1
Soraka_status = SORAKA_HEAL
local player = GetMyHero()
local function findBuff(target, name)
    for i = 1, target.buffCount, 1 do
        local buff = target:GetBuff(i)
        if type(name) == "string" and name:lower() == buff:lower() then return true, buff end
    end
    return false
end

function UltHealMana()
    local wValue = (player:GetSpellData(_W).level < 1 or not player:CanUseSpell(_W) == STATE) and 0 or player:GetSpellData(_W).level * 70 + 0.45 * player.ap
    local eValue = (player:GetSpellData(_E).level < 1 or not player:CanUseSpell(_E) == STATE) and 0 or player:GetSpellData(_E).level * 50 + 0.75 * player.ap
    local rValue = (player:GetSpellData(_R).level < 1 or not player:CanUseSpell(_R) == STATE) and 0 or 80 + player:GetSpellData(_R).level * 120 + 0.7 * player.ap
    local healTarget, manaTarget, effect
    -- check is need heal & ult
    for i = 1, heroManager.iCount, 1 do
        local target = heroManager:getHero(i)
        if target and target.team == player.team and not target.dead then
            local isIgnited = (SetupDontHealIgnited and (findBuff(target, "summonerdot") or findBuff(target, "grievouswound"))) and true or false
            if SetupAutoUlt and target.maxHealth - target.health > rValue and not isIgnited then
                effect = effect and effect + 1 or 1
            elseif rValue > 0 then effect = effect + (target.maxHealth - target.health) / rValue
            end
            -- check is target in distance of Mana and Heal before checking him for Action
            if SetupAutoHeal and player:GetDistance(target) < distance and target.maxHealth - target.health > wValue and not isIgnited then
                if healTarget then if (target.maxHealth - target.health) > (healTarget.maxHealth - healTarget.health) then healTarget = target end
                else healTarget = target
                end
            end
            if SetupAutoMana and target ~= player and target.charName~="Mordekaiser" and (target.maxMana - target.mana) > eValue * SetupManaGiveRate then
                if manaTarget then if (target.maxMana - target.mana) > (manaTarget.maxMana - manaTarget.mana) then manaTarget = target end
                else manaTarget = target
                end
            end
        end
    end

    if healTarget then CastSpell(_W, healTarget) end
    if manaTarget then
        CastSpell(_E, manaTarget)
    end
    if effect and effect > ultEffect and rValue>0 then
        CastSpell(_R)
        PrintChat("<font color='#33FF66'> >> Your ultimate healed for </font>" .. tostring(math.floor(effect * rValue)))
    end
end

function AutoSilence()
    if player:GetSpellLevel(2) > 0 then
        for i = 1, heroManager.iCount, 1 do
            local target = heroManager:getHero(i)
            if target.valid and target.team == TEAM_ENEMY and not target.dead and player:CanUseSpell(_E) == STATE and player:GetDistance(target) < distance then
                if findBuff(target, "DrainChannel") or findBuff(target, "SummonerTeleport") or findBuff(target, "deathlotussound") or
                   findBuff(target, "alzaharnethergraspsound") or findBuff(target, "gate") or findBuff(target, "AbsoluteZero") or findBuff(target, "infiniteduresssound") or findBuff(target, "missfortunebulletsound") then
                    CastSpell(_E, target)
                end
            end
        end
    end
end

--turn off - on
function OnWndMsg(msg, keycode)
    if Activated and keycode == SetupTogleKey and msg == KEY_DOWN then
        if Soraka_status == SORAKA_HEAL then
            Soraka_status = SORAKA_RUN
            PrintChat("<font color='#FF0000'> >> Run Soraka, RUN </font>")
        else
            Soraka_status = SORAKA_HEAL
            PrintChat("<font color='#00FF00'> >> Heal Soraka, HEAL </font>")
        end
    end
end


-- LOAD --
function OnTick()
    if Activated and (not lastUsed or os.clock()-lastUsed>SetupInterval/1000) then
    if Soraka_status == SORAKA_HEAL and SetupAutoSilence then AutoSilence() end
    if Soraka_status == SORAKA_HEAL and (SetupAutoHeal or SetupAutoUlt or SetupAutoMana) then UltHealMana() end
    end
end

function OnLoad()
    if player.charName == "Soraka" then Activated = true
    PrintChat(" >> SORAKA AUTO HEAL")
    end

end
